home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / ARMTEX / SOURCES2 / !TeX / texmf / source / armTeX / web / tangle / ch-final next >
Encoding:
Text File  |  1998-04-04  |  16.0 KB  |  549 lines

  1. @x
  2. \pageno=\contentspagenumber \advance\pageno by 1
  3. @y
  4. \pageno=\contentspagenumber \advance\pageno by 1
  5. \let\maybe=\iffalse
  6. \def\title{TANGLE changes for C}
  7. @z
  8.  
  9. @x
  10. @d banner=='This is TANGLE, Version 4.3'
  11. @y
  12. @d banner=='This is TANGLE, Version 4.3' {more is printed later}
  13. @z
  14.  
  15. @x
  16. @d end_of_TANGLE = 9999 {go here to wrap it up}
  17.  
  18. @p @t\4@>@<Compiler directives@>@/
  19. program TANGLE(@!web_file,@!change_file,@!Pascal_file,@!pool);
  20. label end_of_TANGLE; {go here to finish}
  21. const @<Constants in the outer block@>@/
  22. type @<Types in the outer block@>@/
  23. var @<Globals in the outer block@>@/
  24. @<Error handling procedures@>@/
  25. @y
  26. @d end_of_TANGLE = 9999 {go here to wrap it up}
  27.  
  28. @p program TANGLE;
  29. label end_of_TANGLE; {go here to finish}
  30. const @<Constants in the outer block@>@/
  31. type @<Types in the outer block@>@/
  32. var @<Globals in the outer block@>@/
  33. @<Error handling procedures@>@/
  34. @<Declaration of |scan_args|@>@/
  35. @z
  36.  
  37. @x
  38. @{@&$C-,A+,D-@} {no range check, catch arithmetic overflow, no debug overhead}
  39. @!debug @{@&$C+,D+@}@+ gubed {but turn everything on when debugging}
  40. @y
  41. @=(*$C-*)@> {no range check}
  42. @!debug @=(*$C+*)@>@+ gubed {but turn everything on when debugging}
  43. @z
  44.  
  45. @x
  46. @!stack_size=50; {number of simultaneous levels of macro expansion}
  47. @!max_id_length=12; {long identifiers are chopped to this length, which must
  48.   not exceed |line_length|}
  49. @!unambig_length=7; {identifiers must be unique if chopped to this length}
  50.   {note that 7 is more strict than \PASCAL's 8, but this can be varied}
  51. @y
  52. @!stack_size=100; {number of simultaneous levels of macro expansion}
  53. @!max_id_length=50; {long identifiers are chopped to this length, which must
  54.   not exceed |line_length|}
  55. @!unambig_length=20; {identifiers must be unique if chopped to this length}
  56. @z
  57.  
  58. @x
  59. @d text_char == char {the data type of characters in text files}
  60. @y
  61. @d text_char == ASCII_code {the data type of characters in text files}
  62. @z
  63.  
  64. @x
  65. for i:=1 to @'37 do xchr[i]:=' ';
  66. for i:=@'200 to @'377 do xchr[i]:=' ';
  67. @y
  68. for i:=1 to @'37 do xchr[i]:=chr(i);
  69. for i:=@'200 to @'377 do xchr[i]:=chr(i);
  70. @z
  71.  
  72. @x
  73. @d print(#)==write(term_out,#) {`|print|' means write on the terminal}
  74. @y
  75. @d term_out==stdout
  76. @d print(#)==write(term_out,#) {`|print|' means write on the terminal}
  77. @z
  78.  
  79. @x
  80. @<Globals...@>=
  81. @!term_out:text_file; {the terminal as an output file}
  82. @y
  83. @z
  84.  
  85. @x
  86. @ Different systems have different ways of specifying that the output on a
  87. certain file will appear on the user's terminal. Here is one way to do this
  88. on the \PASCAL\ system that was used in \.{TANGLE}'s initial development:
  89. @^system dependencies@>
  90.  
  91. @<Set init...@>=
  92. rewrite(term_out,'TTY:'); {send |term_out| output to the terminal}
  93. @y
  94. @ Different systems have different ways of specifying that the output on a
  95. certain file will appear on the user's terminal.
  96. @^system dependencies@>
  97.  
  98. @<Set init...@>=
  99.  {Nothing need be done for C.}
  100. @z
  101.  
  102. @x
  103. @d update_terminal == break(term_out) {empty the terminal output buffer}
  104. @y
  105. @d update_terminal == flush(term_out) {empty the terminal output buffer}
  106. @z
  107.  
  108. @x
  109. @ The following code opens the input files.  Since these files were listed
  110. in the program header, we assume that the \PASCAL\ runtime system has
  111. already checked that suitable file names have been given; therefore no
  112. additional error checking needs to be done.
  113. @^system dependencies@>
  114.  
  115. @p procedure open_input; {prepare to read |web_file| and |change_file|}
  116. begin reset(web_file); reset(change_file);
  117. end;
  118. @y
  119. @ The following code opens the input files.
  120. This happens after the |initialize| procedure has executed.
  121. That will have called the |scan_args| procedure to set up the global
  122. variables |web_name| and |chg_name| to the appropriate file
  123. names.
  124. These globals, and the |scan_args| procedure will be defined at the end
  125. where they won't disturb the module numbering.
  126. @^system dependencies@>
  127.  
  128. @p procedure open_input; {prepare to read |web_file| and |change_file|}
  129. begin
  130. reset(web_file,web_name); reset(change_file,chg_name);
  131. end;
  132. @z
  133.  
  134. @x
  135. @ The following code opens |Pascal_file| and |pool|.
  136. Since these files were listed in the program header, we assume that the
  137. \PASCAL\ runtime system has checked that suitable external file names have
  138. been given.
  139. @^system dependencies@>
  140.  
  141. @<Set init...@>=
  142. rewrite(Pascal_file); rewrite(pool);
  143. @y
  144. @ The following code opens |Pascal_file| and |pool|.
  145. Use the |scan_args| procedure to fill the global file names,
  146. according to the names given on the command line.
  147. @^system dependencies@>
  148.  
  149. @<Set init...@>=
  150. scan_args;
  151. rewrite(Pascal_file,pascal_file_name);
  152. @z
  153.  
  154. @x
  155.     begin buffer[limit]:=xord[f^]; get(f);
  156.     incr(limit);
  157.     if buffer[limit-1]<>" " then final_limit:=limit;
  158.     if limit=buf_size then
  159.       begin while not eoln(f) do get(f);
  160. @y
  161.     begin buffer[limit]:=xord[getc(f)];
  162.     incr(limit);
  163.     if buffer[limit-1]<>" " then final_limit:=limit;
  164.     if limit=buf_size then
  165.       begin while not eoln(f) do vgetc(f);
  166. @z
  167.  
  168. @x
  169. @d fatal_error(#)==begin new_line; print(#); error; mark_fatal; jump_out;
  170.   end
  171.  
  172. @<Error handling...@>=
  173. procedure jump_out;
  174. begin goto end_of_TANGLE;
  175. end;
  176. @y
  177. @d jump_out==uexit(1)
  178. @d fatal_error(#)==begin new_line; print(#); error; mark_fatal; uexit(1);
  179.   end
  180. @z
  181.  
  182. @x
  183. @d ww=2 {we multiply the byte capacity by approximately this amount}
  184. @d zz=3 {we multiply the token capacity by approximately this amount}
  185. @y
  186. @d ww=3 {we multiply the byte capacity by approximately this amount}
  187. @d zz=4 {we multiply the token capacity by approximately this amount}
  188. @z
  189.  
  190. @x
  191.     begin if c>="a" then c:=c-@'40; {merge lowercase with uppercase}
  192. @y
  193.     begin
  194. @z
  195.  
  196. @x
  197. @<Define and output a new string...@>=
  198. begin ilk[p]:=numeric; {strings are like numeric macros}
  199. if l-double_chars=2 then {this string is for a single character}
  200.   equiv[p]:=buffer[id_first+1]+@'100000
  201. else  begin equiv[p]:=string_ptr+@'100000;
  202.   l:=l-double_chars-1;
  203. @y
  204. @<Define and output a new string...@>=
  205. begin ilk[p]:=numeric; {strings are like numeric macros}
  206. if l-double_chars=2 then {this string is for a single character}
  207.   equiv[p]:=buffer[id_first+1]+@'100000
  208. else  begin
  209.   if string_ptr = 256 then  rewrite(pool,pool_file_name);
  210.   equiv[p]:=string_ptr+@'100000;
  211.   l:=l-double_chars-1;
  212. @z
  213.  
  214. @x
  215.  (((out_contrib[1]="D")and(out_contrib[2]="I")and(out_contrib[3]="V")) or@|
  216.  ((out_contrib[1]="M")and(out_contrib[2]="O")and(out_contrib[3]="D")) ))or@|
  217. @^uppercase@>
  218. @y
  219.   (((out_contrib[1]="D")and(out_contrib[2]="I")and(out_contrib[3]="V")) or@|
  220.   ((out_contrib[1]="d")and(out_contrib[2]="i")and(out_contrib[3]="v")) or@|
  221.   ((out_contrib[1]="M")and(out_contrib[2]="O")and(out_contrib[3]="D")) or@|
  222.   ((out_contrib[1]="m")and(out_contrib[2]="o")and(out_contrib[3]="d")) ))or@|
  223. @z
  224.  
  225. @x
  226. @^uppercase@>
  227.   if ((out_buf[out_ptr-3]="D")and(out_buf[out_ptr-2]="I")and
  228.     (out_buf[out_ptr-1]="V"))or @/
  229.      ((out_buf[out_ptr-3]="M")and(out_buf[out_ptr-2]="O")and
  230.     (out_buf[out_ptr-1]="D")) then@/ goto bad_case
  231. @y
  232.   if ((out_buf[out_ptr-3]="D")and(out_buf[out_ptr-2]="I")and
  233.     (out_buf[out_ptr-1]="V"))or @/
  234.      ((out_buf[out_ptr-3]="d")and(out_buf[out_ptr-2]="i")and
  235.     (out_buf[out_ptr-1]="v"))or @/
  236.      ((out_buf[out_ptr-3]="M")and(out_buf[out_ptr-2]="O")and
  237.     (out_buf[out_ptr-1]="D"))or @/
  238.      ((out_buf[out_ptr-3]="m")and(out_buf[out_ptr-2]="o")and
  239.     (out_buf[out_ptr-1]="d")) then@/ goto bad_case
  240. @z
  241.  
  242. @x
  243. and_sign: begin out_contrib[1]:="A"; out_contrib[2]:="N"; out_contrib[3]:="D";
  244. @^uppercase@>
  245.   send_out(ident,3);
  246.   end;
  247. not_sign: begin out_contrib[1]:="N"; out_contrib[2]:="O"; out_contrib[3]:="T";
  248.   send_out(ident,3);
  249.   end;
  250. set_element_sign: begin out_contrib[1]:="I"; out_contrib[2]:="N";
  251.   send_out(ident,2);
  252.   end;
  253. or_sign: begin out_contrib[1]:="O"; out_contrib[2]:="R"; send_out(ident,2);
  254. @y
  255. and_sign: begin out_contrib[1]:="a"; out_contrib[2]:="n"; out_contrib[3]:="d";
  256.   send_out(ident,3);
  257.   end;
  258. not_sign: begin out_contrib[1]:="n"; out_contrib[2]:="o"; out_contrib[3]:="t";
  259.   send_out(ident,3);
  260.   end;
  261. set_element_sign: begin out_contrib[1]:="i"; out_contrib[2]:="n";
  262.   send_out(ident,2);
  263.   end;
  264. or_sign: begin out_contrib[1]:="o"; out_contrib[2]:="r"; send_out(ident,2);
  265. @z
  266.  
  267. @x
  268. @ Single-character identifiers represent themselves, while longer ones
  269. appear in |byte_mem|. All must be converted to uppercase,
  270. with underlines removed. Extremely long identifiers must be chopped.
  271.  
  272. (Some \PASCAL\ compilers work with lowercase letters instead of
  273. uppercase. If this module of \.{TANGLE} is changed, it's also necessary
  274. to change from uppercase to lowercase in the modules that are
  275. listed in the index under ``uppercase''.)
  276. @^system dependencies@>
  277. @^uppercase@>
  278.  
  279. @d up_to(#)==#-24,#-23,#-22,#-21,#-20,#-19,#-18,#-17,#-16,#-15,#-14,
  280.   #-13,#-12,#-11,#-10,#-9,#-8,#-7,#-6,#-5,#-4,#-3,#-2,#-1,#
  281.  
  282. @<Cases related to identifiers@>=
  283. "A",up_to("Z"): begin out_contrib[1]:=cur_char; send_out(ident,1);
  284.   end;
  285. "a",up_to("z"): begin out_contrib[1]:=cur_char-@'40; send_out(ident,1);
  286.   end;
  287. identifier: begin k:=0; j:=byte_start[cur_val]; w:=cur_val mod ww;
  288.   while (k<max_id_length)and(j<byte_start[cur_val+ww]) do
  289.     begin incr(k); out_contrib[k]:=byte_mem[w,j]; incr(j);
  290.     if out_contrib[k]>="a" then out_contrib[k]:=out_contrib[k]-@'40
  291.     else if out_contrib[k]="_" then decr(k);
  292.     end;
  293.   send_out(ident,k);
  294.   end;
  295. @y
  296. @ Single-character identifiers represent themselves, while longer ones
  297. appear in |byte_mem|. All must be converted to lowercase,
  298. with underlines removed. Extremely long identifiers must be chopped.
  299. @^system dependencies@>
  300.  
  301. @d up_to(#)==#-24,#-23,#-22,#-21,#-20,#-19,#-18,#-17,#-16,#-15,#-14,
  302.   #-13,#-12,#-11,#-10,#-9,#-8,#-7,#-6,#-5,#-4,#-3,#-2,#-1,#
  303.  
  304. @<Cases related to identifiers@>=
  305. "A",up_to("Z"),
  306. "a",up_to("z"): begin out_contrib[1]:=cur_char; send_out(ident,1);
  307.   end;
  308. identifier: begin k:=0; j:=byte_start[cur_val]; w:=cur_val mod ww;
  309.   while (k<max_id_length)and(j<byte_start[cur_val+ww]) do
  310.     begin incr(k); out_contrib[k]:=byte_mem[w,j]; incr(j);
  311.     if out_contrib[k]="_" then decr(k);
  312.     end;
  313.   send_out(ident,k);
  314.   end;
  315. @z
  316.  
  317. @x
  318. @d add_in(#)==begin accumulator:=accumulator+next_sign*(#); next_sign:=+1;
  319.   end
  320. @y
  321. @d add_in(#)==begin accumulator:=accumulator+next_sign*toint(#); next_sign:=+1;
  322.   end
  323. @z
  324.  
  325. @x
  326. any error stop will set |debug_cycle| to zero.
  327. @y
  328. any error stop will set |debug_cycle| to zero.
  329.  
  330. @d term_in==stdin
  331. @z
  332.  
  333. @x
  334. @!term_in:text_file; {the user's terminal as an input file}
  335. @y
  336.  
  337. @z
  338.  
  339. @x
  340. reset(term_in,'TTY:','/I'); {open |term_in| as the terminal, don't do a |get|}
  341. @y
  342.  
  343. @z
  344.  
  345. @x
  346. print_ln(banner); {print a ``banner line''}
  347. @y
  348. print (banner); {print a ``banner line''}
  349. print_ln (version_string);
  350. @z
  351.  
  352. @x
  353. @<Print the job |history|@>;
  354. @y
  355. @<Print the job |history|@>;
  356. new_line;
  357. if (history <> spotless) and (history <> harmless_message)
  358. then uexit (1)
  359. else uexit (0);
  360. @z
  361.  
  362. @x
  363. This module should be replaced, if necessary, by changes to the program
  364. that are necessary to make \.{TANGLE} work at a particular installation.
  365. It is usually best to design your change file so that all changes to
  366. previous modules preserve the module numbering; then everybody's version
  367. will be consistent with the printed program. More extensive changes,
  368. which introduce new modules, can be inserted here; then only the index
  369. itself will get a new module number.
  370. @^system dependencies@>
  371. @y
  372. This module should be replaced, if necessary, by changes to the program
  373. that are necessary to make \.{TANGLE} work at a particular installation.
  374. It is usually best to design your change file so that all changes to
  375. previous modules preserve the module numbering; then everybody's version
  376. will be consistent with the printed program. More extensive changes,
  377. which introduce new modules, can be inserted here; then only the index
  378. itself will get a new module number.
  379. @^system dependencies@>
  380.  
  381. @ The user calls \.{TANGLE} with arguments on the command line.  These
  382. are either file names or flags (beginning with `\.-').  The following
  383. globals are for communicating the user's desires to the rest of the
  384. program. The various filename variables contain strings with the full
  385. names of those files, as {\mc UNIX} knows them.
  386.  
  387. There are no flags that affect \.{TANGLE} at the moment.
  388.  
  389. @d max_file_name_length==PATH_MAX
  390.  
  391. @<Globals...@>=
  392. @!web_name,@!chg_name,@!pascal_file_name,@!pool_file_name:
  393.         array[1..max_file_name_length] of char;
  394.  
  395. @ The |scan_args| procedure looks at the command line arguments and sets
  396. the |file_name| variables accordingly.  At least one file name must be
  397. present: the \.{WEB} file.  It may have an extension, or it may omit it
  398. to get |'.web'| added.  The \PASCAL\ output file name is formed by
  399. replacing the \.{WEB} file name extension by |'.p'|.  Similarly, the
  400. pool file name is formed using a |'.pool'| extension.
  401.  
  402. If there is another file name present among the arguments, it is the
  403. change file, again either with an extension or without one to get
  404. |'.ch'| An omitted change file argument means that |'/dev/null'| should
  405. be used, when no changes are desired.
  406.  
  407. @<Declaration of |scan_args|@>=
  408. procedure scan_args;
  409.   var dot_pos, slash_pos, i, a: integer; {indices}
  410.   c: char;
  411.   @!fname: array[1..max_file_name_length] of char; {temporary argument holder}
  412.   @!found_web,@!found_change: boolean; {|true| when those file names have
  413.                                         been seen}
  414. begin
  415.   found_web := false;
  416.   found_change := false;
  417.  
  418.   for a := 1 to argc - 1
  419.   do begin
  420.     argv(a,fname); {put argument number |a| into |fname|}
  421.     if fname[1] <> '-'
  422.     then begin
  423.       if not found_web
  424.       then @<Get |web_name|, |pascal_file_name|,
  425.              and |pool_file_name| variables from |fname|@>
  426.       else if not found_change
  427.       then @<Get |chg_name| from |fname|@>
  428.       else  @<Print usage error message and quit@>;
  429.     end else
  430.       @<Handle flag argument in |fname|@>;
  431.   end;
  432.  
  433.   if not found_web then @<Print usage error message and quit@>;
  434.   if not found_change then @<Set up null change file@>;
  435. end;
  436.  
  437. @ Use all of |fname| for the |web_name| if there is a |'.'| in it,
  438. otherwise add |'.web'|.  The other file names come from adding things
  439. after the dot.  The |argv| procedure will not put more than
  440. |max_file_name_length-5| characters into |fname|, and this leaves enough
  441. room in the |file_name| variables to add the extensions.
  442.  
  443. The end of a file name is marked with a |' '|, the convention assumed by
  444. the |reset| and |rewrite| procedures.
  445.  
  446. @<Get |web_name|...@>=
  447. begin
  448.   dot_pos := -1;
  449.   slash_pos := -1;
  450.   i := 1;
  451.   while (fname[i] <> ' ') and (i <= max_file_name_length - 5)
  452.   do begin
  453.     web_name[i] := fname[i];
  454.     if fname[i] = '.' then dot_pos := i;
  455.     if fname[i] = '/' then slash_pos := i;
  456.     incr (i);
  457.   end;
  458.   web_name[i] := ' ';
  459.  
  460.   if (dot_pos = -1) or (dot_pos < slash_pos)
  461.   then begin
  462.     dot_pos := i;
  463.     web_name[dot_pos] :=   '.';
  464.     web_name[dot_pos+1] := 'w';
  465.     web_name[dot_pos+2] := 'e';
  466.     web_name[dot_pos+3] := 'b';
  467.     web_name[dot_pos+4] := ' ';
  468.   end;
  469.  
  470.   for i := 1 to dot_pos
  471.   do begin
  472.     c := web_name[i];
  473.     pascal_file_name[i] := c;
  474.     pool_file_name[i] := c;
  475.   end;
  476.  
  477.   pascal_file_name[dot_pos+1] := 'p';
  478.   pascal_file_name[dot_pos+2] := ' ';
  479.  
  480.   pool_file_name[dot_pos+1] := 'p';
  481.   pool_file_name[dot_pos+2] := 'o';
  482.   pool_file_name[dot_pos+3] := 'o';
  483.   pool_file_name[dot_pos+4] := 'l';
  484.   pool_file_name[dot_pos+5] := ' ';
  485.  
  486.   found_web := true;
  487. end
  488.  
  489. @ @<Get |chg_name|...@>=
  490. begin
  491.   dot_pos := -1;
  492.   slash_pos := -1;
  493.   i := 1;
  494.   while (fname[i] <> ' ') and (i <= max_file_name_length - 5)
  495.   do begin
  496.     chg_name[i] := fname[i];
  497.     if fname[i] = '.' then dot_pos := i;
  498.     if fname[i] = '/' then slash_pos := i;
  499.     incr (i);
  500.   end;
  501.   chg_name[i] := ' ';
  502.  
  503.   if (dot_pos = -1) or (dot_pos < slash_pos)
  504.   then begin
  505.     dot_pos := i;
  506.     chg_name[dot_pos]   := '.';
  507.     chg_name[dot_pos+1] := 'c';
  508.     chg_name[dot_pos+2] := 'h';
  509.     chg_name[dot_pos+3] := ' ';
  510.   end;
  511.  
  512.   found_change := true;
  513. end
  514.  
  515. @ @<Set up null...@>=
  516. begin
  517.         chg_name[1]:='n';
  518.         chg_name[2]:='u';
  519.         chg_name[3]:='l';
  520.         chg_name[4]:='l';
  521.         chg_name[5]:=':';
  522.         chg_name[6]:=' ';
  523. end
  524.  
  525. @ We accept alternate names for the output files via \.{-o} and \.{-p}
  526. flags.
  527.  
  528. @<Handle flag...@>=
  529. begin
  530.   if fname[2] = 'o' then begin
  531.     incr(a);
  532.     argv(a,pascal_file_name);
  533.   end
  534.   else if fname[2] = 'p' then begin
  535.     incr(a);
  536.     argv(a,pool_file_name);
  537.   end
  538.   else
  539.     @<Print usage error message and quit@>;
  540. end
  541.  
  542. @ @<Print usage error message and quit@>=
  543. begin
  544.   print_ln ('Usage: tangle webfile[.web] [changefile[.ch]] [-o file] [-p poolfile]');
  545.   uexit (1);
  546. end
  547. @z
  548.  
  549.